<?php 

class Packing_Resource_UPS extends Packing_Resource_Shippinglabel{
	
	//XML Response
	private $xmlResponse;
	
	//Static Variables
	private $uri = "http://returns.upsrow.com/processxml.php";
	
	//Retailer userid and password
	private $userid  = "beaglebooks";
	private $password = "beaglebooks";
	private $clientid = '810184';
	private $service = '03';
	private $rslevel = '4wrl';
	
	protected $fields = array();
	
	public function __construct($serverType){
//		Currently empty
		parent::__construct();
	}

	public function createFields(){
	
		$buyback = explode("-",$this->getRMA());
		
		$this->fields = array(
			'firstname' => $this->Customer['CustomerName'],
			'company'	=> $this->server["acronym"],
			'address1'	=>	$this->Customer['CustomerAddress2'],
			'address2'	=>	$this->Customer['CustomerAddress1'],
			'shipfromcity'	=>	$this->Customer['CustomerCity'],
			'state'		=>	$this->Customer['CustomerState'],
			'postalcode'=>	$this->Customer['CustomerZip5'],
			'shiptorma'	=>	$buyback[0] . "id" . $buyback[1] . "part" . $buyback[2] ,
			'reference2'=>	$this->server["acronym"],
			'weight1'	=>	$this->Weight['Pounds'],
			'emailaddress'	=> $this->server['adminEmail'],
			'rslevel' => $this->rslevel,
			'service' => $this->service,
			'login_id' => $this->userid,
			'login_password' => $this->password,
			'client' => $this->clientid, 
		);
	}
	
	public function cURLCall(){
		$url = $this->uri;
		
		$this->createfields();

		if(function_exists("curl_init")){
			$ch = curl_init();
			//set URL and other appropriate options
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_HEADER, 0);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_POST,count($this->fields));
			curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0');
			curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($this->fields));
			
			// grab URL and pass it to the browser
			$this->xmlResponse = curl_exec($ch);
			curl_close($ch);
			
			return $this->xmlResponse;
		}else {
			return "cURL not found on server";
		}
	}
	
	public function ParseXML(){
		$this->xmlResponse = explode("\n", $this->xmlResponse); 
		return $this->xmlResponse;
	}
	
	public function checkError(){
		for($i=0; $i<count($this->xmlResponse); $i++){
			if(strstr($this->xmlResponse[$i], "ErrorMessage")) {
	        	if("None" != substr($this->xmlResponse[$i], 14))
	        		return substr($this->xmlResponse[$i], 14);
	        	else
	        		return;
	      	}
		}
	}
	
	public function getPackingSlip(){
		for($i=0; $i<count($this->xmlResponse); $i++){
			if(strstr($this->xmlResponse[$i], "ImageLink")) {
	        	return substr($this->xmlResponse[$i], 11);
	      	}
		}
	}

	public function getTrackingNumber(){
		for($i=0; $i<count($this->xmlResponse); $i++){
			if(strstr($this->xmlResponse[$i], "TrackingNumber")) {
				$result['trackingNumber'] = substr($this->xmlResponse[$i], 16);
			}
		}
	}
}